﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Detai.Admin.Models;
using Detai.Admin.ViewModels;
using Detai.Admin.Components;
namespace Detai.Admin.Controllers
{
    public class SinhvienController : Controller
    {
        private bool isRoled = SystemConstants.Kiemtraquyen(1);
        DetaiDataContext eDataContext = new DetaiDataContext();

        [Authorize()]
        public ActionResult Index()
        {
            if (!isRoled) { return RedirectToAction("Index", "Thongbao", new { message = SystemConstants.Khongcoquyen }); }
            SinhvienIndexViewModel model = new SinhvienIndexViewModel();
            model.Sinhviens = eDataContext.Sinhviens.ToList();
            return View(model);
        }

        [Authorize()]
        public ActionResult Create()
        {
            if (!isRoled) { return RedirectToAction("Index", "Thongbao", new { message = SystemConstants.Khongcoquyen }); }
            SinhvienEditViewModel model = new SinhvienEditViewModel();
            model.Lophocs = eDataContext.Lops.ToList();
            return View(model);
        }


        [HttpPost]
        [Authorize()]
        public ActionResult Create(Lop formData)
        {
            try
            {
                UpdateModel(formData);
                eDataContext.Lops.InsertOnSubmit(formData);
                eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [Authorize()]
        public ActionResult Edit(int id)
        {
            var model = eDataContext.Lops.FirstOrDefault(e => e.Malop == id);
            return View(model);
        }

        [HttpPost]
        [Authorize()]
        public ActionResult Edit(int id, Lop formData)
        {
            try
            {
                formData = eDataContext.Lops.FirstOrDefault(e => e.Malop ==id);
                UpdateModel(formData); 
                eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [Authorize()]
        public ActionResult Delete(int id)
        {
            var item = eDataContext.Lops.FirstOrDefault(e => e.Malop == id);
            return View(item);
        }


        [HttpPost]
        [Authorize()]
        public ActionResult Delete(int id, Lop fromdata)
        {
            try
            {
                var item = eDataContext.Lops.FirstOrDefault(e => e.Malop == id);
                if (item != null)
                {
                    eDataContext.Lops.DeleteOnSubmit(item);
                    eDataContext.SubmitChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
